﻿(function () {
    var myApp = angular.module('MyControllers');

    myApp.controller('roomController', ['$scope', '$interval', '$http', '$modal', 'examRoomService', 'utils', '$routeParams', function ($scope, $interval, $http, $modal, service, utils, $routeParams) {
        var planId = $routeParams.id, srv = service;
        console.log($routeParams);

        $scope.planName = $routeParams.name;

        $scope.getList = function () {
            $scope.loadingState = true;
            srv.getList(planId).success(function (data) {
                $scope.loadingState = false;
                $scope.list = data;
            });
        };

        $scope.setting = function (model) {
            window.location.href = '#/examRoom/setting/' + model.examRoomID + '/' + model.examRoomName + '/' + planId + '/' + $scope.planName;
        };

        $scope.getList();
    }]);

    myApp.controller('roomSetingController', ['$scope', '$location', '$http', '$modal', 'setingRoomService', 'utils', '$routeParams', function ($scope, $location, $http, $modal, service, utils, $routeParams) {
        var roomId = $routeParams.id;
        console.log($routeParams);

        $scope.roomName = $routeParams.roomName;
        $scope.planId = $routeParams.planId;
        $scope.planName = $routeParams.planName;

        $scope.getComputer = function () {
            $scope.loadingState = true;
            service.getComputer(roomId).success(function (data) {
                $scope.list = data;
                $scope.loadingState = false;
                angular.forEach($scope.list, function (data, index, array) {
                    data.modifyIp = false;
                    data.modifyNo = false;
                });
            });
        };
        $scope.modify = function (item, type) {
            if (type == 0) {
                item.modifyIp = true;
            }
        };
        $scope.saveModify = function (item, type) {
            if (type == 0) {
                service.setIp(item.SeatNo, item.ComputerIP).success(function (data) {
                    if (data.resultType == 'Success') {
                        utils.notify('保存成功！', 'success');
                    } else {
                        utils.notify(data.message, 'warning');
                    }

                    // 刷新当前界面
                    $scope.getComputer();
                });
                item.modifyIp = false;
            }
        };
        $scope.modifySeat = function (item, type) {
            if (type == 0) {
                item.modifySeatIp = true;
            }
        };
        $scope.saveModifySeat = function (item, type) {
            if (type == 0) {
                service.updateByComputerIp(item.computerIP, item.seatNo).success(function (data) {
                    if (data.resultType == 'Success') {
                        utils.notify('保存成功！', 'success');
                    } else {
                        utils.notify(data.message, 'warning');
                    }

                    // 刷新当前界面
                    $scope.getComputer();
                });
                item.modifySeatIp = false;
            }
        };

        $scope.addOne = function () {
            $scope.ComputerIP = '';
            $scope.SeatNo = '';

            $scope.visible = true;
            $scope.addComputer = true;
        };
        $scope.saveOne = function () {
            if ($('form#addOneForm').valid()) {
                $scope.loadingState = true;
                service.addone(roomId, $scope.computerIP, $scope.seatNo).success(function (data) {
                    $scope.loadingState = false;
                    if (data.resultType == 'Success') {
                        utils.notify('添加成功！', 'success');
                        $scope.cancelOne();

                        // 刷新当前界面
                        $scope.getComputer();
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                });
            }
        };
        $scope.cancelOne = function () {
            $scope.visible = false;
            $scope.addComputer = false;
        };

        $scope.addMore = function () {
            $scope.computerStartIP = '';
            $scope.computerEndIP = '';
            $scope.seatNO = '';
            $scope.order = 0;

            $scope.addComputer = true;
            $scope.visibles = true;
        };
        $scope.saveMore = function () {
            if ($('form#addMoreForm').valid()) {
                $scope.loadingState = true;
                service.addMore(roomId, $scope.computerStartIP, $scope.computerEndIP, $scope.seatNO, $scope.order).success(function (data) {
                    $scope.loadingState = false;
                    if (data.resultType == 'Success') {
                        utils.notify('批量添加成功！', 'success');
                        $scope.cancelMore();

                        // 刷新当前界面
                        $scope.getComputer();
                    } else {
                        utils.notify(data.message, 'warning');
                    }
                });
            }
        };
        $scope.cancelMore = function () {
            $scope.visibles = false;
            $scope.addComputer = false;
        };

        $scope.checkAll = function (obj) {
            $scope.isCheckAll = !$scope.isCheckAll;

            angular.forEach($scope.list, function (data, index, array) {
                data.isChecked = $scope.isCheckAll;
            });
        };
        $scope.clickLine = function (model) {
            model.isChecked = !model.isChecked;
        };
        $scope.removeComputer = function () {
            var confirm = window.confirm("确认删除选中记录？");
            if (confirm) {
                var removeList = [];
                angular.forEach($scope.list, function (data, index, array) {
                    if (data.isChecked)
                        removeList.push(data.examComputerID);
                });

                if (!removeList.length) {
                    utils.notify('请至少选择一项进行删除操作！', 'warning');
                } else {
                    service.removeComputer(removeList).success(function (data) {
                        switch (data.resultType) {
                            case 'Success':
                                utils.notify(data.message, 'success');
                                $scope.getComputer();
                                break;
                            case 'Warning':
                                utils.notify(data.message, 'warning');
                                break;
                            case 'Error':
                                utils.notify(data.message, 'error');
                                break;
                        }
                    });
                }

                $scope.isCheckAll = false;
            }
        };

        $scope.getComputer();
    }]);
})();